"""Rename environment_build table to environment_builds

Revision ID: 1dc3553c4e7e
Revises: ba1fc5af0aed
Create Date: 2021-01-27 11:26:55.464949

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "1dc3553c4e7e"
down_revision = "ba1fc5af0aed"
branch_labels = None
depends_on = None


def upgrade():
    op.drop_index(
        "ix_environment_build_environment_uuid", table_name="environment_build"
    )
    op.drop_index("ix_environment_build_project_path", table_name="environment_build")
    op.drop_index("ix_environment_build_project_uuid", table_name="environment_build")
    op.drop_index("uuid_proj_env_index", table_name="environment_build")
    op.drop_table("environment_build")

    op.create_table(
        "environment_builds",
        sa.Column("uuid", sa.String(length=36), nullable=False),
        sa.Column("project_uuid", sa.String(length=36), nullable=False),
        sa.Column("environment_uuid", sa.String(length=36), nullable=False),
        sa.Column("project_path", sa.String(length=4096), nullable=False),
        sa.Column("requested_time", sa.DateTime(), nullable=False),
        sa.Column("started_time", sa.DateTime(), nullable=True),
        sa.Column("finished_time", sa.DateTime(), nullable=True),
        sa.Column("status", sa.String(length=15), nullable=True),
        sa.PrimaryKeyConstraint("uuid", name=op.f("pk_environment_builds")),
        sa.UniqueConstraint("uuid", name=op.f("uq_environment_builds_uuid")),
    )
    op.create_index(
        op.f("ix_environment_builds_environment_uuid"),
        "environment_builds",
        ["environment_uuid"],
        unique=False,
    )
    op.create_index(
        op.f("ix_environment_builds_project_path"),
        "environment_builds",
        ["project_path"],
        unique=False,
    )
    op.create_index(
        op.f("ix_environment_builds_project_uuid"),
        "environment_builds",
        ["project_uuid"],
        unique=False,
    )
    op.create_index(
        "uuid_proj_env_index",
        "environment_builds",
        ["project_uuid", "environment_uuid"],
        unique=False,
    )


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "environment_build",
        sa.Column("uuid", sa.VARCHAR(length=36), autoincrement=False, nullable=False),
        sa.Column(
            "project_uuid", sa.VARCHAR(length=36), autoincrement=False, nullable=False
        ),
        sa.Column(
            "environment_uuid",
            sa.VARCHAR(length=36),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "project_path", sa.VARCHAR(length=4096), autoincrement=False, nullable=False
        ),
        sa.Column(
            "requested_time",
            postgresql.TIMESTAMP(),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "started_time", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
        sa.Column(
            "finished_time", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
        sa.Column("status", sa.VARCHAR(length=15), autoincrement=False, nullable=True),
        sa.PrimaryKeyConstraint("uuid", name="pk_environment_build"),
    )
    op.create_index(
        "uuid_proj_env_index",
        "environment_build",
        ["project_uuid", "environment_uuid"],
        unique=False,
    )
    op.create_index(
        "ix_environment_build_project_uuid",
        "environment_build",
        ["project_uuid"],
        unique=False,
    )
    op.create_index(
        "ix_environment_build_project_path",
        "environment_build",
        ["project_path"],
        unique=False,
    )
    op.create_index(
        "ix_environment_build_environment_uuid",
        "environment_build",
        ["environment_uuid"],
        unique=False,
    )
    op.drop_index("uuid_proj_env_index", table_name="environment_builds")
    op.drop_index(
        op.f("ix_environment_builds_project_uuid"), table_name="environment_builds"
    )
    op.drop_index(
        op.f("ix_environment_builds_project_path"), table_name="environment_builds"
    )
    op.drop_index(
        op.f("ix_environment_builds_environment_uuid"), table_name="environment_builds"
    )
    op.drop_table("environment_builds")
    # ### end Alembic commands ###
